const path = require('path');
const nodeExternals = require('webpack-node-externals');

module.exports = {
  target: 'node',
  mode: process.env.NODE_ENV || 'production',
  entry: './src/app.ts',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'app.js',
    clean: true,
  },
  resolve: {
    extensions: ['.ts', '.js'],
    alias: {
      '@': path.resolve(__dirname, 'src'),
      '@/controllers': path.resolve(__dirname, 'src/controllers'),
      '@/models': path.resolve(__dirname, 'src/models'),
      '@/routes': path.resolve(__dirname, 'src/routes'),
      '@/validators': path.resolve(__dirname, 'src/validators'),
      '@/constants': path.resolve(__dirname, 'src/constants'),
      '@/config': path.resolve(__dirname, 'src/config'),
      '@/utils': path.resolve(__dirname, 'src/utils'),
      '@/middlewares': path.resolve(__dirname, 'src/middlewares'),
      '@/databases': path.resolve(__dirname, 'src/databases'),
      '@/types': path.resolve(__dirname, 'src/types'),
    },
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  externals: [nodeExternals()],
  devtool: process.env.NODE_ENV === 'development' ? 'source-map' : false,
  optimization: {
    minimize: process.env.NODE_ENV === 'production',
  },
  stats: {
    colors: true,
    modules: false,
    children: false,
    chunks: false,
    chunkModules: false,
  },
};